package com.qf.dao.impl;

import com.qf.dao.AddressDao;
import com.qf.pojo.Address;
import com.qf.utils.DataSourceUtils;
import com.qf.utils.TxUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

/**
 * @author 千锋教育
 * @Company http://www.mobiletrain.org/
 * @Version 1.0
 */
public class AddressDaoImpl implements AddressDao {

    @Override
    public List<Address> findAddListByUid(int uid) {
        QueryRunner qf = new QueryRunner(DataSourceUtils.getDataSource());
        String sql = "select id , detail, name , phone, uid, level from tb_address where uid=?";
        try {
            List<Address> addList = qf.query(sql, new BeanListHandler<>(Address.class), uid);

            return addList;
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("查询地址列表失败!!");
        }
        return null;
    }

    @Override
    public void insert(Address address) {
        String sql = "INSERT INTO `tb_address` (`id`, `detail`, `name`, `phone`, `uid`, `level`) VALUES (null, ?, ?, ?, ?, ?)";
        QueryRunner qf = new QueryRunner(DataSourceUtils.getDataSource());
        try {
            qf.update(sql , address.getDetail(), address.getName(), address.getPhone(), address.getUid(), address.getLevel());
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("新增失败!!");
        }
    }

    @Override
    public void deleteById(String id) {
        String sql = "delete from tb_address where id = ?";
        QueryRunner qf = new QueryRunner(DataSourceUtils.getDataSource());
        try {
            qf.update(sql , id);
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("删除失败!!");
        }
    }

    /**
     * 线程
     *  ThreadLocal: 线程的副本对象， 可以存储对象
     *
     * conn.setAutoCommit(false)
     *  执行多次sql
     * conn.commit()
     * conn.rollback();
     *
     * conn.setAutoCommit(true);
     * @param address
     */
    @Override
    public void update(Address address) {
        String sql = "UPDATE `tb_address` SET `detail`=?, `name`=?, `phone`=?, `uid`=?, `level`=? WHERE (`id`=?)";
        QueryRunner qf = new QueryRunner(DataSourceUtils.getDataSource());
        try {
            qf.update(sql , address.getDetail(), address.getName(), address.getPhone(),address.getUid(),address.getLevel(), address.getId());
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("修改失败!!");
        }
    }

    @Override
    public void clearDefaultAddress(int uid) {
        Connection conn = TxUtils.getConn();
        System.out.println(conn);
        String sql = "UPDATE `tb_address` SET `level`= 0 WHERE (`uid`=?)";
        QueryRunner qf = new QueryRunner();
        try {
            qf.update(conn, sql , uid);
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("修改失败!!");
        }
    }

    @Override
    public void setDefalutAddress(String aid) {
        Connection conn = TxUtils.getConn();
        System.out.println(conn);
        String sql = "UPDATE `tb_address` SET `level`= 1 WHERE (`id`=?)";
        QueryRunner qf = new QueryRunner(DataSourceUtils.getDataSource());
        try {
            qf.update(conn, sql , aid);
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("修改失败!!");
        }
    }
}
